package www.webols.utils;

import java.io.IOException;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


public class CustomSSLSocketFactory extends
		org.apache.http.conn.ssl.SSLSocketFactory {
	private SSLSocketFactory FACTORY = HttpsURLConnection
			.getDefaultSSLSocketFactory();
	public SSLSocketFactory getFACTORY() {
		return FACTORY;
	}

	

	private X509TrustManager xtm = new X509TrustManager() {
		public void checkClientTrusted(X509Certificate[] chain, String authType) {
		}

		public void checkServerTrusted(X509Certificate[] chain, String authType) {
			System.out.println("cert: " + chain[0].toString() + ", authType: "
					+ authType);
		}

		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}
	};
	public CustomSSLSocketFactory() throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException {
		super(null);
		try {
			SSLContext context = SSLContext.getInstance("TLS");
			TrustManager[] tm = new TrustManager[] { xtm};
			context.init(null, tm, new SecureRandom());

			FACTORY = context.getSocketFactory();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Socket createSocket() throws IOException {
		return FACTORY.createSocket();
	}

	// TODO: add other methods like createSocket() and getDefaultCipherSuites().
	// Hint: they all just make a call to member FACTORY
}